package com.shujia.dim

import com.shujia.common.utils.SparkTool
import org.apache.spark.sql.{DataFrame, SaveMode, SparkSession}

object DimUsertagMskDay extends SparkTool {
  /**
   * 基于ods的用户画像数据进行脱敏
   */
  override def run(spark: SparkSession): Unit = {
    import spark.implicits._
    import org.apache.spark.sql.functions._

    val odsUserTagDF: DataFrame = spark.table("ods.ods_usertag_d")

    // 分别对mdn手机号、name姓名、id_number身份证号进行脱敏
    odsUserTagDF
      .where($"day_id" === day_id)
      .select(
        upper(md5(concat($"mdn", expr("'shujia'")))) as "mdn"
        , upper(md5(concat($"name", expr("'shujia'")))) as "name"
        , $"gender"
        , $"age"
        , upper(md5(concat($"id_number", expr("'shujia'")))) as "id_number"
        , $"number_attr"
        , $"trmnl_brand"
        , $"trmnl_price"
        , $"packg"
        , $"conpot"
        , $"resi_grid_id"
        , $"resi_county_id"
      )
      .write
      .format("csv")
      .option("sep", ",")
      .mode(SaveMode.Overwrite)
      .save(s"/daas/motl/dim/dim_usertag_msk_d/day_id=$day_id")

    // 添加分区
    spark.sql(
      s"""
         |alter table dim.dim_usertag_msk_d  add if not exists partition(day_id='$day_id')
         |""".stripMargin)
  }
}
